package com.cetc.nsfx.controller.sys;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cetc.nsfx.security.vo.UserContext;
import com.cetc.nsfx.entity.SysMenu;
import com.cetc.nsfx.service.sys.StationService;
import com.cetc.nsfx.entity.SysRoles;
import com.cetc.nsfx.base.BaseController;

@Controller
@RequestMapping("/sys/station")
public class StationController  extends BaseController {
	
	private StationService stationService;

	@Autowired
	public void setStationService(
			StationService stationService) {
		this.stationService = stationService;
	}

	
	@RequestMapping("/toStationList")
	public String toStationList(){
		return "/sys/stationList";
	}
	/**
	 * 岗位列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		String loginName = user.getUserId();
		int iDisplayLength = Integer.valueOf(request.getParameter("iDisplayLength"));	
		int iDisplayStart = Integer.valueOf(request.getParameter("iDisplayStart"));
		String sSearch = request.getParameter("searchText");
		Map<String, Object> rs = stationService.loadRoleList(loginName,sSearch,iDisplayStart,iDisplayLength);
		Map<String,Object> roleData = new HashMap<String,Object>();
		roleData.put("iTotalRecords",rs.get("count"));
		roleData.put("iTotalDisplayRecords", rs.get("count"));
		roleData.put("aaData", rs.get("rs"));
		return roleData;	
	}
	
	/**
	 * 岗位模板信息
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "roleModelList", method = RequestMethod.GET)
	@ResponseBody
	public void roleModelList(HttpServletRequest request,HttpServletResponse response) {
		UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
//		Map<String, Object> reValue = new HashMap<String, Object>();
		String roletype = request.getParameter("roletype");
		List<Map<String,Object>> pageData = stationService.loadSysRoleModel(user.getUsername(),roletype);
		
//		reValue.put("aaData",pageData);
		sendJSON(response, "{\"aaData\":"+JSON.toJSONString(pageData)+"}");
	}
	
	/**
	 * 新增/修改岗位
	 * @param request
	 * @param respose
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public void saveOrUpdate(HttpServletRequest request,HttpServletResponse respose,@RequestParam("menuID[]") String[] menuID) {
		
		Map<String,Object> map=new HashMap<String,Object>();
		
		String roleCode = request.getParameter("ROLE_CODE");
		String roleName = request.getParameter("ROLE_NAME");
		String roleType = request.getParameter("ROLE_TYPE");
		String parentCode = request.getParameter("PARENT_CODE");
		String roleDesc = request.getParameter("ROLE_DESC");
		
		int result= stationService.saveOrUpdate(roleCode,roleName,roleType,parentCode,menuID,roleDesc);
		if(result==1){
			map.put("success", "success");
		}else if(result==-1){
			map.put("success", "existed");
		}else{
			map.put("success", "failed");
		}
		sendJSON(respose, JSON.toJSONString(map));
	}
	
	/**
	 * 岗位删除
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> deleteSysRole(HttpServletRequest request) {
		
		Map<String,Object> map=new HashMap<String,Object>();
		String roleCode = request.getParameter("ROLE_CODE");
		
		int result= stationService.delete(roleCode);
		if(result==1){
			map.put("success", "success");
		}else{
			map.put("success", "failed");
		}
		return map;
	}
	
	/**
	 * 岗位菜单信息保存更新
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/saveRoleMenu", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> saveOrUpdateRoleMenu(HttpServletRequest request,@RequestParam("menuID[]") String[] menuID) {
		
		Map<String,Object> map=new HashMap<String,Object>();
		System.out.println(menuID);
		
		
		String roleCode = request.getParameter("roleCode");//角色编码
//		int result= stationService.saveOrUpdateRoleMenu(menuID,roleCode);
//		if(result==1){
//			map.put("success", "success");
//		}else{
//			map.put("success", "failed");
//		}
		return map;
	}
	
	/**
	 * 系统菜单信息（包括已选状态）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/sysmenu",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getMenus(HttpServletRequest request){
		 Map<String,Object> model = new HashMap<String,Object>(); 
		 UserContext user = (UserContext) SecurityUtils.getSubject().getPrincipal();
		 Map<String,Object> paramMap = new HashMap<String,Object>();
		 String type = request.getParameter("type");
		 paramMap.put("menuType", "1");
		 //String rolecode = request.getParameter("rolecode").toString();
		 paramMap.put("roleCode",request.getParameter("ROLE_CODE")==null?"":request.getParameter("ROLE_CODE").toString());
		 paramMap.put("userName",user.getUserId());
		 paramMap.put("userType", user.getUser().get("USER_TYPE"));
		 //根据权限判断菜单是否显示,设置enabled属性
		 List<SysMenu> menuList = stationService.getSysMenu(paramMap);
		model.put("menus", menuList);
		return model;
	}
	
	public void sendJSON(HttpServletResponse response, String json) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.getOutputStream().write(json.getBytes("UTF-8"));
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}
	
	/**
	 * 根据岗位编码查询菜单名称
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getRoleMenuName",method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getRoleMenuName(HttpServletRequest request){
		 Map<String,Object> model = new HashMap<String,Object>(); 
		 String roleCode = request.getParameter("ROLE_CODE");
		 List<Map<String,Object>> menuList = stationService.getRoleMenuName(roleCode);
		 model.put("menus", menuList);
		return model;
	}
	
	
	
	
}
